OpenRoads Designer CONNECT Edition SDK Help

Arc creator

Description

  • This is a custom interactive tool for creating arc using 3 points.
  • The user is prompted for three points which user needs to select from UI.
  • After the first 2 point selection, they are linked by a temporarily drawn line. After the third point, an arc is created with the three points.
  • The ArcCreator class which extends DgnElementSetTool which handles different events to interact with UI, the ArcCreator class overrides the events here in this tool.
  • The method OnDataButton() handles the point selection from User interface.

Remarks

  • This sample code is a part of ManagedSDKExample which you get with SDK installation under "examples" section in SDK installation directory.
  • If you encounter any error while using DgnElementSetTool class, make sure to add a reference to Bentley.DgnDisplayNet.dll by selecting Project > Add Reference or change the projects .csproj file to add reference to this dll .
  • The default dll location will be "C:\Program Files\Bentley\OpenRoads Designer CE 10.11\OpenRoadsDesigner\Bentley.DgnDisplayNet.dll"

Source Code



//Required References
using System.Collections.Generic;
using Bentley.DgnPlatformNET;
using Bentley.DgnPlatformNET.Elements;
using Bentley.GeometryNET;
using Bentley.MstnPlatformNET;
using Bentley.CifNET.SDK.Edit;
using Bentley.CifNET.GeometryModel.SDK.Edit;
using Bentley.CifNET.GeometryModel.SDK;

namespace ManagedSDKExample
    {
    class ArcCreator : DgnElementSetTool
        {
        internal static DgnModel activeModel = Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModel();
        internal static ModelInfo info = activeModel.GetModelInfo();

        private double UOR_TO_MASTER = 1.0 / info.UorPerMeter;
        List<DPoint3d> m_points = new List<DPoint3d>();

        /*----------------------------------------------------------------------------------------------**/
        /* Write Function | The user is prompted for three points, after which an arc is created that passes
        *                  through all the points. The first two points are linked by a temporarily drawn line.
        /*--------------+---------------+---------------+---------------+---------------+----------------*/
        internal void CreateArcFromPoints()
            {
            if (m_points.Count < 2)
                {
                return;
                }

            //adjusts x and y values, unit conversions
            DPoint3d startPoint = new DPoint3d(m_points[0]);
            startPoint.X *= UOR_TO_MASTER;
            startPoint.Y *= UOR_TO_MASTER;
            startPoint.Z *= UOR_TO_MASTER;
            DPoint3d thruPoint = new DPoint3d(m_points[1]);
            thruPoint.X *= UOR_TO_MASTER;
            thruPoint.Y *= UOR_TO_MASTER;
            thruPoint.Z *= UOR_TO_MASTER;
            DPoint3d endPoint = new DPoint3d(m_points[2]);
            endPoint.X *= UOR_TO_MASTER;
            endPoint.Y *= UOR_TO_MASTER;
            endPoint.Z *= UOR_TO_MASTER;

            //creates new arc
            Bentley.CifNET.LinearGeometry.CircularArc arc = Bentley.CifNET.LinearGeometry.CircularArc.Create3(startPoint, endPoint, thruPoint);

            ConsensusConnectionEdit con = ConsensusConnectionEdit.GetActive();

            GeometricModel gm = con.GetOrCreateGeometricModel();

            if (gm == null)
                {
                con.Close();
                con.Dispose();
                return;
                }

	           //Creates alignment using the created arc
            con.StartTransientMode();
            Alignment al = gm.CreateAlignmentByLinearElement(arc, true);
            con.PersistTransients();
            }

        protected override void OnPostInstall()
            {
            NotificationManager.OutputPrompt("Select first data point.");
            BeginDynamics();
            }
        
        //adds points on click
        protected override bool OnDataButton(DgnButtonEvent ev)
            {
            if (m_points.Count == 0)
                {
                m_points.Add(ev.Point);
                BeginDynamics();
                NotificationManager.OutputPrompt("Select second data point.");
                }
            else if (m_points.Count == 1)
                {
                m_points.Add(ev.Point);
                NotificationManager.OutputPrompt("Select third data point.");
                }
            else if (m_points.Count == 2)
                {
                m_points.Add(ev.Point);
                CreateArcFromPoints();
                EndDynamics();
                m_points.Clear();
                NotificationManager.OutputPrompt("Select first data point or pick element selection tool to exit command.");
                }
            
            return false;
            }

        //allows preview lines to be drawn
        protected override void OnDynamicFrame(DgnButtonEvent ev)
            {
            RedrawElems redraw = new RedrawElems();
            redraw.DrawMode = DgnDrawMode.TempDraw;
            redraw.DrawPurpose = DrawPurpose.Dynamics;
            redraw.SetViewport(ev.Viewport);

            if (m_points.Count > 1)
                {
                LineStringElement lse = new LineStringElement(Bentley.MstnPlatformNET.Session.Instance.GetActiveDgnModel(), null, m_points.ToArray());
                redraw.DoRedraw(lse);
                }
            }

        protected override void OnRestartTool()
            {
            InstallNewInstance();
            }

        public override StatusInt OnElementModify(Element element)
            {
            return StatusInt.Error;
            }

        public static void InstallNewInstance()
            {
            ArcCreator tool = new ArcCreator();
            tool.InstallTool();
            }
        }
    }